package com.qf.realm;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.jdbc.JdbcRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Test;

public class JdbcRealmTest {

    @Test
    public void Jdbc(){
        //1.创建Realm
        JdbcRealm realm=new JdbcRealm();
            //1.2创建数据源
        DruidDataSource dataSource=new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql:///shiro");
        dataSource.setUsername("root");
        dataSource.setPassword("12345678");
        realm.setDataSource(dataSource);
            //1.3自定义sql
        realm.setAuthenticationQuery("select password from user where username = ?");
            //1.4手动开启校验权限
        realm.setPermissionsLookupEnabled(true);
        //2.创建SecurityManager
        DefaultSecurityManager securityManager = new DefaultSecurityManager();
        securityManager.setRealm(realm);
        //3.创建Subject
        SecurityUtils .setSecurityManager(securityManager);
        Subject subject=SecurityUtils.getSubject();
        //4.提交认证请求
        subject.login(new UsernamePasswordToken("admin","admin"));
        //5.角色的授权
        subject.checkRoles("超级管理员");
        //6.权限的给予
        subject.checkPermissions("product:insert");

    }
}
